package mww.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.entity.Score;
import mww.javabean.entity.Student;
import mww.javabean.service.StudentCService;
import mww.javabean.service.StudentCServiceImpl;
import mww.javabean.service.StudentService;
import mww.javabean.service.StudentServiceImpl;

public class GradeServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String action = request.getParameter("action");
		if (action.equals("calScore")) {
			doCalScore(request, response); // 计算所有学生的总学分、总绩点

		} else if (action.equals("calRank")) {
			doCalRank(request, response); // 计算所有学生的班级排名、专业排名
		}
	}

	private void doCalScore(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();// 获得一个Session对象，用于存放一些提示信息返回到前台或者获取Session对象中的信息
		StudentCService scService = new StudentCServiceImpl();
		StudentService stuService = new StudentServiceImpl();
		try {
			ArrayList list = (ArrayList) stuService.findAllStudent();	//取得所有学生信息

			int n = list.size();
			Student stu[] = new Student[n];
			while (n-- > 0) {	//循环次数为 所有学生个数
				stu[n] = (Student) list.get(n);	//从list总取出学生
				Score score = new Score();
				score = scService.calAll(stu[n].getStudentNo());	//对每位学生计算所选课程总学分、总绩点、平均绩点
				
				// 按学号更新学生的总学分、总绩点、平均绩点
				stuService.updateScore(stu[n].getStudentNo(), score.getSumCredit(), score.getSumCreditGPA(),
						score.getAvgScoreGPA());
			}
			session.setAttribute("message", "分数汇总成功！");
			request.getRequestDispatcher("/admin/dealWithScore.jsp").forward(request, response);
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}

	private void doCalRank(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();// 获得一个Session对象，用于存放一些提示信息返回到前台或者获取Session对象中的信息
		StudentCService scService = new StudentCServiceImpl();
		StudentService stuService = new StudentServiceImpl();
		try {
			Vector studentMajor = stuService.findAllMajor(); // 查询出所有专业
			int majorNum = studentMajor.size();
			for (int i = 0; i < majorNum; i++) {	//对每个专业循环一次
				ArrayList studentList = new ArrayList();

				//取出第i个专业转化为String作为查询学生的参数,
				studentList = (ArrayList) stuService.findAllStudentByMajor((String)studentMajor.elementAt(i));
				int studentNum = studentList.size();
				Student stu[] = new Student[studentNum];	//创建同一个专业的学生数组
				while (studentNum-- > 0) {	//循环更新学生的专业排名
					stu[studentNum] = (Student) studentList.get(studentNum);

					//由于查询出的studentList 按平均基地降序排列，故最后一名同学为最后一名
					stuService.updateMajorRank(stu[studentNum].getStudentNo(), studentNum+1);	//列表中第stu[0]个为第一名（0+1）	
				}
			}

			Vector studentClass = stuService.findAllClass(); // 查询出所有班级
			int classNum = studentClass.size();
			for (int j = 0; j < classNum; j++) {	//对每个班级循环一次
				ArrayList studentList = new ArrayList();
				//取出第j个班级转化为String作为查询学生的参数,
				studentList = (ArrayList) stuService.findAllStudentByClass((String)studentClass.elementAt(j));
				int studentNum = studentList.size();
				Student stu[] = new Student[studentNum];	//创建同一个班级的学生数组
				while (studentNum-- > 0) {	//循环更新学生的班级排名
					stu[studentNum] = (Student) studentList.get(studentNum);
					//由于查询出的studentList 按平均基地降序排列，故最后一名同学为最后一名
					stuService.updateClassRank(stu[studentNum].getStudentNo(), studentNum+1);	//列表中第stu[0]个为第一名（0+1）	
				}
			}
			
			session.setAttribute("message", "排名成功！");
			request.getRequestDispatcher("/admin/dealWithScore.jsp").forward(request, response);
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}
	public static void main(String rags[]){
		System.out.println("debug");
	}
}
